package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.domain.OrderDetail;
import org.example.dto.OrderDto;
import org.example.domain.Orders;
import org.example.domain.R;
import org.example.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("ok");
    }

    @GetMapping("/userPage")
    public R<Page<OrderDto>> page(int page, int pageSize) {
        Page<OrderDto> page1 = ordersService.page(page, pageSize);
        return R.success(page1);
    }

    @GetMapping("/page")
    public R<Page> list(int page, int pageSize, Long number, String beginTime, String endTime) {
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (number != null) {
            ordersLambdaQueryWrapper.like(Orders::getNumber, number);
        }
        if (beginTime != null && endTime != null) {
            ordersLambdaQueryWrapper.between(Orders::getOrderTime, beginTime, endTime);
        }
        System.out.println(beginTime);
        System.out.println(endTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        ordersService.page(ordersPage, ordersLambdaQueryWrapper);
        return R.success(ordersPage);
    }
}
